using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp_ChenxiConsole.FileIO;

namespace WpfApp_ChenxiConsole.Controls
{
    /// <summary>
    /// LoadedFilesViewer.xaml 的交互逻辑
    /// </summary>
    public partial class LoadedFilesViewer : UserControl
    {
        /// <summary>
        /// 关闭按钮点击事件
        /// </summary>
        public event RoutedEventHandler? CloseButtonClick;

        public LoadedFilesViewer()
        {
            InitializeComponent();
            InitializeEvents();
        }

        private void InitializeEvents()
        {
            // 绑定关闭按钮事件
            CloseButton.Click += (s, e) =>
            {
                CloseButtonClick?.Invoke(this, e);
            };

            // 绑定文件选择事件
            FilesListView.SelectionChanged += FilesListView_SelectionChanged;
        }

        /// <summary>
        /// 设置已加载文件列表
        /// </summary>
        /// <param name="files">文件列表</param>
        public void SetFiles(List<LoadedFileInfo> files)
        {
            FilesListView.Items.Clear();

            if (files != null && files.Count > 0)
            {
                foreach (var fileInfo in files)
                {
                    FilesListView.Items.Add(fileInfo);
                }
                FilesListView.IsEnabled = true;
            }
            else
            {
                // 如果没有已加载的文件，显示提示信息
                FilesListView.Items.Add(new { FileName = "暂无已加载文件", FileType = "", FileSize = "", LoadTime = "" });
                FilesListView.IsEnabled = false;
            }
        }

        private void FilesListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (FilesListView.SelectedItem is LoadedFileInfo selectedFile)
            {
                // 转换字节数组为16进制字符串
                StringBuilder hexBuilder = new();
                hexBuilder.AppendLine($"文件名: {selectedFile.FileName}");
                hexBuilder.AppendLine($"文件类型: {selectedFile.FileType}");
                hexBuilder.AppendLine($"文件大小: {selectedFile.FileSize} 字节");
                hexBuilder.AppendLine($"加载时间: {selectedFile.LoadTime}");
                hexBuilder.AppendLine();
                hexBuilder.AppendLine("16进制内容:");
                hexBuilder.AppendLine();

                int bytesPerLine = 16;
                int address = 0;

                // 按行格式化16进制内容
                while (address < selectedFile.FileContent.Length)
                {
                    int lineLength = Math.Min(bytesPerLine, selectedFile.FileContent.Length - address);
                    hexBuilder.AppendFormat("{0:X8}: ", address);

                    // 16进制部分
                    for (int i = 0; i < bytesPerLine; i++)
                    {
                        if (i < lineLength)
                        {
                            hexBuilder.AppendFormat("{0:X2} ", selectedFile.FileContent[address + i]);
                        }
                        else
                        {
                            hexBuilder.Append("   "); // 补齐空位
                        }
                    }

                    // ASCII部分
                    hexBuilder.Append(" | ");
                    for (int i = 0; i < lineLength; i++)
                    {
                        byte b = selectedFile.FileContent[address + i];
                        // 显示可打印字符，否则显示点
                        hexBuilder.Append((b >= 32 && b <= 126) ? (char)b : '.');
                    }

                    hexBuilder.AppendLine();
                    address += bytesPerLine;
                }

                HexContentTextBox.Text = hexBuilder.ToString();
            }
        }
    }
}